package mx.itesm.escenas;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.text.Text;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import mx.itesm.constants.ConstantesEscenas;
import mx.itesm.manejadores.ManejadorEscenas;
import mx.itesm.manejadores.ManejadorRecursos;


public class EscenaScores extends EscenaBase {
	
	/*
	 * **************************************************************
	 *                                         Variables
	 * **************************************************************
	 */

	private Text txtScoreLV1;
	private Text txtScoreLV2;
	private Text txtScoreLV3;

	private static final int ORIGEN = ConstantesEscenas.ORIGEN;
	
	private static final String PROGRESS = "Progress";
	private static final String LV1 = "Lv1_Score";
	private static final String LV2 = "Lv2_Score";

	/*
	 * **************************************************************
	 *                                         Metodos
	 * **************************************************************
	 */
	
	private int leerScores(String nivel) {
		SharedPreferences prefs = gActivity.getSharedPreferences(PROGRESS, 0);
		int score = prefs.getInt(nivel, 0);
		return score;
	}
	
	@Override
	public void crearEscena() {
		attachChild(createSprite(ORIGEN, ORIGEN, manejadorRecursos.scoresRegion,
				mVerticesObjetos));
		
		int scoreLV1 = leerScores(LV1);
		int scoreLV2 = leerScores(LV2);
		String levelOneStr = "Score:" + scoreLV1;
		String levelTwoStr = "Score:" + scoreLV2;
		
		Font tipoLetra = FontFactory.create(engine.getFontManager(),
				engine.getTextureManager(), 256, 256,
				Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 20f, true, Color.BLACK);
		tipoLetra.load();
		tipoLetra.prepareLetters("TimeScore: 1234567890.".toCharArray());

		txtScoreLV1 = new Text(220, 295, tipoLetra, levelOneStr, 15, 
			ManejadorRecursos.getInstance().mVerticesObjetos);
		txtScoreLV2 = new Text(380, 180, tipoLetra, levelTwoStr, 15, 
				ManejadorRecursos.getInstance().mVerticesObjetos);
		txtScoreLV3 = new Text(545, 65, tipoLetra, "Score:TBA", 15, 
				ManejadorRecursos.getInstance().mVerticesObjetos);

		this.attachChild(txtScoreLV1);
		this.attachChild(txtScoreLV2);
		this.attachChild(txtScoreLV3);
	}

	@Override
	public TipoEscena getTipoEscena() {
		return TipoEscena.ESCENA_INSTRUCT;
	}

	@Override
	public void removerEscena() {
		this.detachSelf();
		this.dispose();
	}

	@Override
	public void onBackKeyPressed() {

		ManejadorEscenas.getInstance().cargarEscenaLoading();
		ManejadorEscenas.getInstance().setEscena(TipoEscena.ESCENA_CARGAR);
		ManejadorEscenas.getInstance().descargarEscenaScores();
		
		engine.registerUpdateHandler(new TimerHandler(0.2f, new ITimerCallback() {

			@Override
			public void onTimePassed(final TimerHandler pTimerHandler) {

				engine.unregisterUpdateHandler(pTimerHandler);
				ManejadorEscenas.getInstance().cargarEscenaMenu();
				ManejadorEscenas.getInstance().setEscena(TipoEscena.ESCENA_MENU);
			}
		}));
	}	
}
